package com.xpgk.mvc.statistic.turnoverRatioJdrStatistic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Service;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.codeSet.Code;
import com.xpgk.mvc.statistic.turnoverRatioStoreStatistic.TurnoverRatioStoreStatisticSQL;

@Service(name = TurnoverRatioJdrStatisticServices.serviceName)
public class TurnoverRatioJdrStatisticServices  extends  BaseService{
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(TurnoverRatioJdrStatisticServices.class);
	public static final String serviceName = "turnoverRatioJdrStatisticServices";
	
	/**获取顾客成交率列表
	 * 
	 * @param startTime
	 * @param endTime
	 * @param organIds
	 * @param gkfl
	 * @return
	 */
	public  List<Record>  getTurnoverRatioJdr(String startTime,String endTime,String organIds,String gkfl)
	{
		Map<String, Object> queryParam = new HashMap<String, Object>();
	    queryParam.put("startTime", startTime);
		queryParam.put("endTime", endTime);
		queryParam.put("organIds", organIds);
		queryParam.put("gkfl", gkfl);
		List<Record> cuslist = find(TurnoverRatioJdrStatisticSQL.sqlId_getTurnoverRatioJdr, queryParam);
		Record  record=new Record();
		record.set("jdrids", "-1");
		record.set("jdrname", "合计");
		long intonum = 0;
		long dealnum = 0;
		long nodealnum = 0;
		if(cuslist!=null && cuslist.size()>0)
		{
			for(Record re:cuslist)
			{
				intonum+=re.getLong("intonum")==null?0:re.getLong("intonum");
				dealnum+=re.getLong("dealnum")==null?0:re.getLong("dealnum");
				nodealnum+=re.getLong("nodealnum")==null?0:re.getLong("nodealnum");
			}
		}
		record.set("organids", organIds);
		record.set("intonum", intonum);
		record.set("dealnum", dealnum);
		record.set("nodealnum", nodealnum);
		if(cuslist!=null)
		{
			cuslist.add(record);
		}
		return  cuslist;
	}
	/**获取未成交原因
	 * 
	 * @param startTime
	 * @param endTime
	 * @param jdrids
	 * @param organIds
	 * @return
	 */
	public  List<Record> getReasonRatio(String startTime,String endTime,String date,String jdrids,String organIds)
	{
		
		if(jdrids.equals("-1"))
		{
			    Map<String, Object> queryParam = new HashMap<String, Object>();
			    queryParam.put("startTime", startTime);
				queryParam.put("endTime", endTime);
				queryParam.put("organIds", organIds);
				List<Record> result = find(TurnoverRatioStoreStatisticSQL.sqlId_getReasonInfo, queryParam);
				return result;
		}else
		{	
			Map<String, Object> queryParam = new HashMap<String, Object>();
		    queryParam.put("organIds", organIds);
			queryParam.put("gkfl", Code.cjCus_ids);
			queryParam.put("startTime", startTime);
			queryParam.put("endTime", endTime);
			queryParam.put("jdrids", jdrids);
			if(date!=null && !date.equals("合计"))
			{
				queryParam.put("date", date);
			}
			List<Record> result = find(TurnoverRatioJdrStatisticSQL.sqlId_getReasonRatio, queryParam);
			return result;
		}
	}
	
	/**导出
	 * 
	 * @param startTime
	 * @param endTime
	 * @param result
	 * @param organIds
	 * @return
	 */
	public  List<Record>  excel(String startTime,String endTime,List<Record> result,String organIds)
	{
		if(result!=null && result.size()>0)
		{
			for(int i=0;i<result.size();i++)
			{	
				//获取未成交原因
				List<Record> wcjbl=  getReasonRatio(startTime,endTime,null,result.get(i).getStr("jdrids"),organIds);
				if(wcjbl!=null && wcjbl.size()>0)
				{	
					//
					for(Record wcjRecord:wcjbl)
					{	
						float  value=0;
						if(result.get(i).getLong("nodealnum")!=null)
						{
							value=(float)wcjRecord.getLong("value")/(float)result.get(i).getLong("nodealnum")*100;
						}
						result.get(i).set(wcjRecord.getStr("ids")+"_bl",  String.format("%.2f", value)+"%");
					}
					if(result.get(i).getLong("dealnum")==null)
					{
						result.get(i).set("dealnum", 0L);
					}
					if(result.get(i).getLong("nodealnum")==null)
					{
						result.get(i).set("nodealnum", 0L);
					}
					float  cjl=0;
					if(result.get(i).getLong("dealnum")!=null && result.get(i).getLong("intonum")!=null)
					{
						cjl=(float)result.get(i).getLong("dealnum")/(float)result.get(i).getLong("intonum")*100;
					}
					//设置成交率
					result.get(i).set("dealratio",  String.format("%.2f", cjl)+"%");
				}
			}
		}
		
		return  result;
	}
	
	public List<Record>  lookReasonInfo(String jdrids,String organIds,String startTime,String endTime,String gkfl)
	{
		 	Map<String, Object> queryParam = new HashMap<String, Object>();
		    queryParam.put("startTime", startTime);
			queryParam.put("endTime", endTime);
			queryParam.put("organIds", organIds);
			queryParam.put("jdrids", jdrids);
			queryParam.put("gkfl", gkfl);
			List<Record> result = find(TurnoverRatioJdrStatisticSQL.sqlId_lookReasonInfo, queryParam);
			
			Record  record=new Record();
			record.set("date", "合计");
			long intonum = 0;
			long dealnum = 0;
			long nodealnum = 0;
			if(result!=null && result.size()>0)
			{
				for(Record re:result)
				{
					intonum+=re.getLong("intonum")==null?0:re.getLong("intonum");
					dealnum+=re.getLong("dealnum")==null?0:re.getLong("dealnum");
					nodealnum+=re.getLong("nodealnum")==null?0:re.getLong("nodealnum");
				}
			}
			record.set("intonum", intonum);
			record.set("dealnum", dealnum);
			record.set("nodealnum", nodealnum);
			if(result!=null)
			{
				result.add(record);
			}
			return result;
	}
	
}
